﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using TierApplication;
using DevExpress.XtraGrid.Columns;



namespace OrderMgr.OrderInfo
{
    public partial class frmSaleShipStockProductDetail : TierService.frmTemplate
    {
        public TierService.Utility.OperateMode OpMode;
        public string _POrderID;//销售单号
        public string _POrderDetailID;//入库单明细
        public string _DepotID;//出库公司
        public string _OrderID;//出库编号
        public string _ProductID;//商品编号
        public string _OrderDetailID;//本次出库明细
        public string _SaleDetailID;
        public double _NeedOutQty;

        // public TierService.Utility.OrderProductType _orderProductType;

        public frmSaleShipStockProductDetail()
        {
            InitializeComponent();
        }

        private void frmOrderProductDetail_Load(object sender, EventArgs e)
        {
            //gcSaleProduct.ForceInitialize();
            //AutoHeightHelper helper = new AutoHeightHelper(gvSaleProduct);
            //helper.EnableColumnPanelAutoHeight();
            if (_DepotID != null)
            {
                c_Depot mDepot = new c_Depot();
                mDepot.GetModel(_DepotID);
                txtDepotName.Text = mDepot.DepotName;
            }

            txtNeedOutQty.Text = _NeedOutQty.ToString();
            switch (OpMode)
            {
                case Utility.OperateMode.Insert:
                    txtPOrderID.Text = _POrderID;
                    txtOrderID.Text = _OrderID;
                    txtProductID.Text = _ProductID;

                    queryInfo(_ProductID);
                    break;
                case Utility.OperateMode.Query:
                     txtPOrderID.Text = _POrderID;
                    txtOrderID.Text = _OrderID;
                    txtProductID.Text = _ProductID;

                    queryInfo(_ProductID);
                    gbStockOut.Visible = false;
                    gcSaleProduct.Height = 400;
                    btnSave.Visible = false;
                    break;
                case Utility.OperateMode.Update:
                    txtOrderDetailID.Text = _OrderDetailID;
                    queryInfoByOrderDetailID(_OrderDetailID);
                    break;

                default:
                    break;
            }

            ckbOnlySelf.Checked = true;
            gvSaleProduct.BestFitColumns();
        }

        private void queryInfo(string sProductID)
        {
            DataSet ds = T_Order.GetSaleShipStockProductByProductID(sProductID);
            gcSaleProduct.DataSource = ds.Tables[0];

            gvSaleProduct_Click(null, null);

        }

        private void queryInfoByOrderDetailID(string sOrderDetailID)
        {
            T_OrderDetail mOrderDetail = new T_OrderDetail();
            mOrderDetail.GetModel(sOrderDetailID);
            //txtOrderDetailID.Text = sOrderDetailID;

            //string sPOrderDetailID = mOrderDetail.POrderDetailID;
            //DataSet ds = T_Order.GetGetSaleShipStockProductByOrderDetailID(sPOrderDetailID);
            DataSet ds = T_Order.GetGetSaleShipStockProductByOrderDetailID(sOrderDetailID);
            gcSaleProduct.DataSource = ds.Tables[0];
            gvSaleProduct_Click(null, null);

            txtOrderID.Text = mOrderDetail.OrderID;

            txtActQty.Value = mOrderDetail.ActQuantity.Value;
            txtBatchNo.Text = mOrderDetail.BatchNo;
            txtDepotArea.Text = mOrderDetail.DepotArea;
            txtUsefulLifeType.EditValue = mOrderDetail.UsefulLifeType;
            if (mOrderDetail.UsefulLife != null)
                txtUsefulLife.DateTime = mOrderDetail.UsefulLife.Value;

        }




        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            T_OrderDetail mOrderDetail = new T_OrderDetail();
            //if (mOrderDetail.ExistsPOrderDetailID(txtBuyStockOrderDetailID.Text,txtOrderID.Text))
            //{
            //    TierService.Utility.Msg_Prompt("本次采购入库单已在本出库单中，不能再行出库。请在单据下修改数量");
            //    return;
            //}

            int iDm = 0;
            string sSm = "";
            if (txtOrderDetailID.Text == "")
            {
                T_Order.CheckStockOutByStockInOrderDetailID(txtBuyStockOrderDetailID.Text, txtOrderID.Text,int.Parse(txtActQty.Value.ToString()), ref iDm, ref sSm);
            }
            else
            {
                T_Order.CheckStockOutByOrderDetailID(_OrderDetailID, int.Parse(txtActQty.Value.ToString()), ref iDm, ref sSm);
            }
            if (iDm < 0)
            {
                MessageBox.Show(sSm);
                return;

            }


            //if (!CheckStockQty())
            //    return;
            mOrderDetail.GetModel(txtOrderDetailID.Text);
            mOrderDetail.POrderDetailID = txtBuyStockOrderDetailID.Text;//采购入库明细单号
            mOrderDetail.OrderID = txtOrderID.Text;//销售出库单号
            mOrderDetail.PSaleDetailID = _SaleDetailID;
            mOrderDetail.ProductID = txtProductID.Text;
            mOrderDetail.ProductName = txtProductName.Text;
            mOrderDetail.ProductNo = txtProductNo.Text;
            mOrderDetail.PlnQuantity = txtActQty.Value;
            mOrderDetail.ActQuantity = txtActQty.Value;
            mOrderDetail.BckQuantity = 0;
            mOrderDetail.BatchNo = txtBatchNo.Text;
            mOrderDetail.CreateDate = DateTime.Now;
            mOrderDetail.CreateUser = Utility.gUserID;
            mOrderDetail.DepotArea = txtDepotArea.Text;
            mOrderDetail.UsefulLifeType = txtUsefulLifeType.Text;
            if (txtUsefulLife.Text != "")
                mOrderDetail.UsefulLife = txtUsefulLife.DateTime;
            //采购商品价格
            T_OrderDetail mBuyOrderDetail = new T_OrderDetail();
            mBuyOrderDetail.GetModel(mOrderDetail.POrderDetailID);
            mOrderDetail.UnitPriceM = mBuyOrderDetail.UnitPrice;
            //销售商品价格
            T_OrderDetail mSaleOrderDetail = new T_OrderDetail();
            mSaleOrderDetail.GetModel(_SaleDetailID);
            mOrderDetail.UnitPrice = mSaleOrderDetail.UnitPrice;
            mOrderDetail.SumPrice = Math.Round(mSaleOrderDetail.UnitPrice.Value * txtActQty.Value, 2);//出库商品的原价格

            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mOrderDetail.OrderDetailID = Utility.GetSeqNextval(Utility.SeqName.OrderDetailID.ToString()).ToString();
                        mOrderDetail.RelaID = mOrderDetail.GetOrderDetailRelaID(mOrderDetail.OrderDetailID, mOrderDetail.POrderDetailID);
                        mOrderDetail.Add();
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("商品信息新增成功!");
                    DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        mOrderDetail.OrderDetailID = txtOrderDetailID.Text;
                        mOrderDetail.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("商品信息修改出错,错误原因为:" + ex.Message);
                        break;
                    }


                    MessageBox.Show("商品信息修改成功!");
                    DialogResult = DialogResult.OK;
                    break;
                default:
                    break;
            }
        }

        private void txtUsefulLifeType_EditValueChanged(object sender, EventArgs e)
        {
            if ((string)txtUsefulLifeType.EditValue == "长期")
            {
                txtUsefulLife.Visible = false;
            }
            else
            {
                txtUsefulLife.Visible = true;
            }
        }

        private void gvSaleProduct_Click(object sender, EventArgs e)
        {
            if (gvSaleProduct.RowCount < 1) return;
            DataRow dr = gvSaleProduct.GetDataRow(gvSaleProduct.FocusedRowHandle);
            string sOrderDetailID = dr["OrderDetailID"].ToString();
            txtActQuantity.Text = dr["ActQuantity"].ToString();
            txtStockAuditedQuantity.Text = dr["StockAuditedQuantity"].ToString();
            txtStockAuditingQuantity.Text = dr["StockAuditingQuantity"].ToString();
            txtCurQuantity.Text = dr["CurQuantity"].ToString();

            txtBuyOrderID.Text = dr["BuyOrderID"].ToString();
            txtBuyStockOrderID.Text = dr["BuyStockOrderID"].ToString();
            txtBuyStockOrderDetailID.Text = dr["OrderDetailID"].ToString();


            T_OrderDetail mOrderDetail = new T_OrderDetail();
            mOrderDetail.GetModel(sOrderDetailID);
            txtProductID.Text = mOrderDetail.ProductID;
            txtProductName.Text = mOrderDetail.ProductName;
            txtBatchNo.Text = mOrderDetail.BatchNo;
            txtDepotArea.Text = mOrderDetail.DepotArea;
            txtProductNo.Text = mOrderDetail.ProductNo;

            txtUsefulLifeType.EditValue = mOrderDetail.UsefulLifeType;
            if (mOrderDetail.UsefulLife != null)
                txtUsefulLife.DateTime = mOrderDetail.UsefulLife.Value;
        }

        private void txtActQty_EditValueChanged(object sender, EventArgs e)
        {
            CheckStockQty();
        }

        private bool CheckStockQty()
        {
            decimal dActQty = txtActQty.Value;
            if (gvSaleProduct.RowCount < 1) return true;
            DataRow dr = gvSaleProduct.GetDataRow(gvSaleProduct.FocusedRowHandle);
            string sCurQuantity = dr["CurQuantity"].ToString();

            decimal dCurQuantity = decimal.Parse(sCurQuantity);
            if (dActQty > dCurQuantity)
            {
                XtraMessageBox.Show("出库数量大于当前库存数量,请重新录入出库数量");
                txtActQty.Focus();
                return false;
            }
            return true;
        }

        private void btnQueryTOStock_Click(object sender, EventArgs e)
        {
            if (gvSaleProduct.RowCount < 1) return;
            DataRow dr = gvSaleProduct.GetDataRow(gvSaleProduct.FocusedRowHandle);
            string sID = dr["FirstOrderID"].ToString();

            Rpt.OrderFunc.QueryOrder(sID);
            //frmBuyToStockAuditDetail fDetail = new frmBuyToStockAuditDetail();
            //fDetail.OpMode = TierService.Utility.OperateMode.Query;
            //fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            //fDetail._OrderType = Utility.OrderType.BuyToStock;
            //fDetail._OrderID = sID;
            //fDetail.ShowDialog();
        }

        private void btnQury_Click(object sender, EventArgs e)
        {
            if (gvSaleProduct.RowCount < 1) return;
            DataRow dr = gvSaleProduct.GetDataRow(gvSaleProduct.FocusedRowHandle);
            string sID = dr["BuyStockOrderID"].ToString();

            Rpt.OrderFunc.QueryOrder(sID);
        }

        private void ckbOnlySelf_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbOnlySelf.Checked)
                gvSaleProduct.Columns["DeportIDName"].FilterInfo = new ColumnFilterInfo("[DeportIDName] = '"+txtDepotName.Text +"'");
            if (!ckbOnlySelf.Checked)
                gvSaleProduct.Columns["DeportIDName"].FilterInfo = new ColumnFilterInfo("[DeportIDName] like  '%%");
            gvSaleProduct_Click(null, null);
        }

    }

}